from django.db import models
# Create your models here.

class Grader(models.Model):
    Name = models.CharField(max_length=100)
    Email = models.EmailField(unique=True)
    Type = models.CharField(max_length=1)
    def __unicode__(self):
        return unicode(self.Name)
    
class Student(models.Model):
    Name = models.CharField(max_length=100)
    RollNo = models.CharField(max_length=20)
    Email = models.EmailField(max_length=50, unique=True)
    def __unicode__(self):
        return unicode(self.Name)

class Course(models.Model):
    CourseNo = models.CharField(max_length=30)
    Name = models.CharField(max_length=40)
    SubEmail = models.EmailField(max_length=40)
    SubPassword = models.TextField()
    Graders = models.ManyToManyField(Grader)
    Students = models.ManyToManyField(Student)
    Year = models.IntegerField()
    Semester = models.CharField(max_length=10)
    def __unicode__(self):
        return unicode(self.Name)
    
    class Meta:
        unique_together = ('CourseNo', 'Year', 'Semester')
    
class Assignment(models.Model):
    SubjectLine = models.CharField(max_length=50)
    StartDateTime = models.DateTimeField()
    EndDateTime = models.DateTimeField()
    course = models.ForeignKey(Course)
    MaxMarks = models.IntegerField()
    def __unicode__(self):
        return unicode(self.SubjectLine)
    
    class Meta:
        unique_together = ('SubjectLine', 'course')

class Question(models.Model):
    ParentQuestion = models.ForeignKey('self', null=True)
    assignment = models.ForeignKey(Assignment)
    QNo = models.IntegerField()
    def __unicode__(self):
        if(self.ParentQuestion==None):
            return unicode(str(self.QNo))
        else:
            return unicode(str(str(self.ParentQuestion.QNo) + '.' + str(self.QNo)))
        
    
    class Meta:
        unique_together = ('assignment', 'QNo', 'ParentQuestion')
    
class QuestionGrade(models.Model):
    submission = models.ForeignKey('Submission')
    question = models.ForeignKey(Question)
    Marks = models.FloatField()
    def __unicode__(self):
        return unicode(self.Marks)
    
    class Meta:
        unique_together = ('submission', 'question')
    
class Submission(models.Model):
    student = models.ForeignKey(Student)
    assignment = models.ForeignKey(Assignment)
    grader = models.ForeignKey(Grader, null=True)
    ReceivedDateTime = models.DateTimeField()
    Grade_Timestamp = models.DateTimeField(null=True)
    Comment = models.TextField(null=True)
    Status = models.CharField(max_length=10)
    Filename = models.TextField(null=True)
    BonusPenaltyType = models.CharField(max_length=1, null=True)
    BonusPenaltyValue = models.FloatField(null=True)
    EmailUID = models.IntegerField()
    def __unicode__(self):
        return unicode(self.student.Name)
    
    class Meta:
        unique_together = ('student', 'assignment')
